package com.huawei123.hasee.cniao5store.mvp.view.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.huawei123.hasee.cniao5store.R;
import com.huawei123.hasee.cniao5store.adapter.section.CategoryContactsSection;
import com.huawei123.hasee.cniao5store.adapter.top.CategoryTopWrapper;
import com.huawei123.hasee.cniao5store.base.BaseMvpFragment;
import com.huawei123.hasee.cniao5store.bean.CategoryBean;
import com.huawei123.hasee.cniao5store.mvp.presenter.impl.CategoryPresenterImpl;
import com.huawei123.hasee.cniao5store.mvp.view.view.CategoryFragmentView;
import com.huawei123.hasee.cniao5store.utils.UIUtils;
import com.huawei123.hasee.cniao5store.view.LoadingPager;
import com.huawei123.hasee.cniao5store.view.widget.ViewUpSearch;
import com.zhxu.recyclerview.section.SectionRVAdapter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：Cniao5Store
 * 类描述：分类界面
 * 创建人：Administrator
 * 创建时间：2017/10/25 0025 下午 4:58
 * 修改人：Administrator
 * 修改时间：2017/10/25 0025 下午 4:58
 * 修改备注：
 */

public class CategoryFragment extends BaseMvpFragment<CategoryPresenterImpl> implements CategoryFragmentView {
    @BindView(R.id.mRV_category)
    RecyclerView mRV_category;
    @BindView(R.id.mVUS_search)
    ViewUpSearch mVUS_search;

    @Inject
    public CategoryPresenterImpl categoryPresenter;
    private CategoryBean categoryBean;
    //判定搜索框是否展开
    private boolean isUnfold = true;

    @Override
    protected void loading() {
        categoryPresenter.getCategpryFragmentData(myActivity);

    }

    @Override
    protected View createSuccessView() {
        View view = UIUtils.inflate(R.layout.fragment_category);
        ButterKnife.bind(this, view);

        SectionRVAdapter sectionRVAdapter = new SectionRVAdapter(getContext());
        CategoryContactsSection categoryContactsSection = new CategoryContactsSection(getContext(), categoryBean.getTitle(),
                categoryBean.getCategoryDataBeanList());
        sectionRVAdapter.addSection(categoryContactsSection);
        CategoryTopWrapper categoryTopWrapper = new CategoryTopWrapper(getContext(), sectionRVAdapter);

        categoryTopWrapper.addAllData(categoryBean.getCategoryTopBeanList());

        mRV_category.setLayoutManager(new LinearLayoutManager(getContext()));
        mRV_category.setAdapter(categoryTopWrapper);
        mRV_category.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) mRV_category.getLayoutManager();
                //RecyclerView滚动显示到的条目号，从顶部算第一条（头部控件数为0）
                int itemPosition = layoutManager.findFirstVisibleItemPosition();
                //我们只在头部四按钮 处理展开搜索栏
                if (itemPosition == 0 && dy > 0 && isUnfold) {//dy>0 ecyclerView正在向上滚动，搜索栏是打开状态 我们锁紧搜索栏
                    mVUS_search.updateShow(!isUnfold);
                    //                    isUnfold=!isUnfold;
                    isUnfold = false;
                } else if (itemPosition == 0 && dy < 0 && !isUnfold) {//dy<0 RecyclerView正在向下滚动，搜索栏是关闭状态 我们打开搜索栏
                    mVUS_search.updateShow(!isUnfold);
                    //                    isUnfold=!isUnfold;
                    isUnfold = true;
                }
            }
        });
        return view;
    }

    @Override
    public void requestDataSuccess(CategoryBean categoryBean) {
        this.categoryBean = categoryBean;
        setState(LoadingPager.LoadResult.success);
    }

    @Override
    public void requestDataError(String errmsg) {
        setState(LoadingPager.LoadResult.error);
    }

    @Override
    public void requestDataEmpty() {
        setState(LoadingPager.LoadResult.empty);
    }

    @Override
    protected CategoryPresenterImpl initPresenterInjector() {
        //使用父类BaseMvpFragment实例化好的ragmentComponent对象注入本Fragment对象
        fragmentComponent.inject(this);
        //返回注入的Presenter
        return categoryPresenter;
    }
}
